﻿
@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none;">
            <label id="AddSampleGroup-SampleGroupId">@ViewData["SampleGroupId"]</label>
            <label id="AddSampleGroup-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">实验室和组*</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="AddSampleGroup-Site" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
            <select id="AddSampleGroup-Group" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="AddSampleGroup-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="AddSampleGroup-Type" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="AddSampleGroup-Input-Type" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="AddSampleGroup-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <br />
    <div class="text-right">
        <button id="AddSampleGroup-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="AddSampleGroup-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>

    $(document).ready(function() {
        LoadSampleGroupTypeList('AddSampleGroup-Type');
        LoadSiteList('AddSampleGroup-Site');
        if ($("#AddSampleGroup-SampleGroupId").html().length > 0) {
            LoadSampleGroup();
        }
    });

    function LoadSampleGroup() {
        if ($("#AddSampleGroup-SampleGroupId").html().length > 0) { 
            $.post("GetSampleGroup", {
                SampleGroupId: $("#AddSampleGroup-SampleGroupId").html()
            }, function (data) {
                $("#AddSampleGroup-Site").val(data.site);
                $("#AddSampleGroup-Name").val(data.name);
                $("#AddSampleGroup-Input-Type").val(data.type);
                $("#AddSampleGroup-Description").val(data.description);
                LoadGroupList(data.site, 'AddSampleGroup-Group', data.group);
            });
        }
    }

    $("#AddSampleGroup-Type").change(function () {
        $("#AddSampleGroup-Input-Type").val($("#AddSampleGroup-Type").find("option:selected").text());
    });

    $("#AddSampleGroup-Site").change(function () {
        LoadGroupList($("#AddSampleGroup-Site").find("option:selected").text(), 'AddSampleGroup-Group');
    });

    $("#AddSampleGroup-confirm").click(function () {
        if ($("#AddSampleGroup-Name").val().length == 0) {
            alert("样品组名称不能为空！");
            $("#AddSampleGroup-Name").addClass('error-style');
            return false;
        }
        if ($("#AddSampleGroup-Input-Type").val().length == 0) {
            alert("样品组类型不能为空！");
            $("#AddSampleGroup-Input-Type").addClass('error-style');
            return false;
        }
        if ($("#AddSampleGroup-Site").find("option:selected").text().length == 0) {
            alert("样品组所属实验室不能为空！");
            $("#AddSampleGroup-Site").addClass('error-style');
            return false;
        }
        if ($("#AddSampleGroup-Group").find("option:selected").text().length == 0) {
            alert("样品组所属工作组不能为空！");
            $("#AddSampleGroup-Group").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Name"] = $("#AddSampleGroup-Name").val();
        jsonStr["Type"] = $("#AddSampleGroup-Input-Type").val();
        jsonStr["Site"] = $("#AddSampleGroup-Site").find("option:selected").text();
        jsonStr["Group"] = $("#AddSampleGroup-Group").find("option:selected").text();
        jsonStr["Description"] = $("#AddSampleGroup-Description").val();

        if ($("#AddSampleGroup-SampleGroupId").html().length == 0) {
            $.post("postAddSampleGroup", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                if (data.indexOf("成功") >= 0) {
                    $("#AddSampleGroup-cancel").click();
                }
            });
        } else {
            jsonStr["Id"] = $("#AddSampleGroup-SampleGroupId").html();
            $.post("postUpdateSampleGroup", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                var modalID = $("#AddSampleGroup-Parent-ModalID").html();
                $("#" + modalID).modal('hide');
            });
        }

    });

    $("#AddSampleGroup-cancel").click(function () {
        var modalID = $("#AddSampleGroup-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });
    
</script>
